package m.jclecai.com.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioGroup;

import butterknife.BindView;
import butterknife.OnClick;
import m.jclecai.com.R;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.constant.Constant_Result;
import m.jclecai.com.fragment.DiscoverSwitchFragment;
import m.jclecai.com.fragment.GroupBuyFragment;
import m.jclecai.com.fragment.RecommendFragment;
import m.jclecai.com.fragment.UserSwitchFragment;
import m.jclecai.com.fragment.home.HomeSwitchFragment;

/**
 *   程序主页面  （开关）
 */
public class MainSwitchActivity extends BaseActivity {

    @BindView(R.id.viewBody)
    View viewBody;

    @BindView(R.id.rg_tab)
    RadioGroup rg_tab;

    private UserSwitchFragment userFragment;
    private HomeSwitchFragment homeFragment;
//    private HomeFragment_ homeFragment;
    private GroupBuyFragment buyFragment;
    //开奖信息；
//    private LotteryFragment lotteryFragment;
    //走势图
 //   private LotteryTrendFragment trendFragment;
//    private FollowFragment trendFragment;

    private RecommendFragment recommendFragment;
    private DiscoverSwitchFragment discoverFragment;

    @Override
    public int getContentView() {
        return R.layout.activity_main_test;
    }

    public void selectHomeTab(){
        selectHomeTab(R.id.rb_home);
    }

    public void selectHomeTab(int checkedId){
        int oCid = rg_tab.getTag() == null? -1 : Integer.parseInt(rg_tab.getTag().toString());
        if (checkedId == R.id.rb_home && oCid != R.id.rb_home) {
            //首页
            if(homeFragment == null)
            homeFragment = new HomeSwitchFragment();
            homeFragment.requestData("football");
            repalce(homeFragment);
        } else if (checkedId == R.id.rb_group_buy && oCid != R.id.rb_group_buy) {
            //合买中心
            if (buyFragment == null)
                buyFragment = new GroupBuyFragment();
            repalce(buyFragment);
        } else if (checkedId == R.id.rb_lottery && oCid != R.id.rb_lottery) {
            //开奖信息
            if (discoverFragment == null)
                discoverFragment = new DiscoverSwitchFragment();
            repalce(discoverFragment);
//            if(lotteryFragment==null){
//                lotteryFragment=new LotteryFragment();
//            }
//            repalce(lotteryFragment);
        } else if (checkedId == R.id.rb_trend && oCid != R.id.rb_trend) {
            //走势图
          if (recommendFragment == null)
                recommendFragment = new RecommendFragment();
            repalce(recommendFragment);
//            if(trendFragment==null){
//                trendFragment=new FollowFragment();
//            }
//            repalce(trendFragment);
        } else if (checkedId == R.id.rb_user && oCid != R.id.rb_user) {
            //用户中心
        /*    if (!isLogin()) {
                startActivityForResult(new Intent(MainActivity.this, LoginActivity.class), Constant_Result.SHOWUSERFRAGMENT);
                return;
            } else {
                if (userFragment == null)
                    userFragment = new UserFragment();
                repalce(userFragment);
            }*/
            if (userFragment == null)
                userFragment = new UserSwitchFragment();
            repalce(userFragment);
        }
        //默认设置选中的viewtag
        rg_tab.setTag(checkedId);
        rg_tab.check(checkedId);
    }

    @OnClick({R.id.rb_home, R.id.rb_group_buy, R.id.rb_lottery, R.id.rb_trend, R.id.rb_user})
    void showFragment(View view) {
        selectHomeTab(view.getId());
    }

    @Override
    public void initView() {
        selectHomeTab(R.id.rb_home);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (Constant_Result.SHOWUSERFRAGMENT == requestCode) {
            selectHomeTab(Integer.parseInt(rg_tab.getTag().toString()));
        }
        if (getCurrentFragment() != null)
            getCurrentFragment().onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        //不保存fragment状态避免重叠
    //  super.onSaveInstanceState(outState);
    }
}
